﻿package Game.Utils{
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.display.Loader;
	import flash.events.*;
	import Game.Utils.T;
	
	public class FileLoader{
		public function FileLoader():void{
		}
		private static var m_fileArray:Array = new Array();
		public static function LoadObject(_adress:String,_callBackFunction:Function, _fileFormat:String):void{
			var p_array:Array = new Array();
			
			p_array[0] = _adress;
			p_array[1]= _callBackFunction;
			p_array[2]= _fileFormat
			
			m_fileArray.push(p_array);
			
			if(m_fileArray.length>1){
				return;				
			}else{
				startFileDownload();
			}			
			
			
		}
		private static var fileLoader:*;
		private static function startFileDownload():void{
			if(m_fileArray[0][2]=='swf'){
				fileLoader = new Loader();
				fileLoader.contentLoaderInfo.addEventListener ( Event.COMPLETE, OnFileLoaded );
				fileLoader.contentLoaderInfo.addEventListener ( HTTPStatusEvent.HTTP_STATUS, OnFileHTTPError);
				fileLoader.contentLoaderInfo.addEventListener ( IOErrorEvent.IO_ERROR,OnFileLoadingIOError );
				fileLoader.load(new URLRequest (m_fileArray[0][0]));
			}
			if(m_fileArray[0][2]=='xml'){
				fileLoader = new URLLoader()
				fileLoader.dataFormat= URLLoaderDataFormat.TEXT;
				fileLoader.addEventListener ( Event.COMPLETE, OnFileXmlLoaded );
				fileLoader.addEventListener ( HTTPStatusEvent.HTTP_STATUS, OnFileHTTPError);
				fileLoader.addEventListener ( IOErrorEvent.IO_ERROR,OnFileLoadingIOError );
				fileLoader.load(new URLRequest (m_fileArray[0][0]));
			}
		}
		
		//////////////////////////////////////////
		//Fonction de traitement de données XML //
		//////////////////////////////////////////
		private static function OnFileLoaded(_evt:Event):void{
			fileLoader.contentLoaderInfo.removeEventListener( Event.COMPLETE, OnFileLoaded );
			fileLoader.contentLoaderInfo.removeEventListener ( HTTPStatusEvent.HTTP_STATUS, OnFileHTTPError);
			fileLoader.contentLoaderInfo.removeEventListener ( IOErrorEvent.IO_ERROR,OnFileLoadingIOError );
			//T.Trace(fileLoader.content);
			endFileLoading(fileLoader.content);
		}
		
		private static function OnFileXmlLoaded(_evt:Event):void{
			fileLoader.removeEventListener( Event.COMPLETE, OnFileXmlLoaded );
			fileLoader.removeEventListener ( HTTPStatusEvent.HTTP_STATUS, OnFileHTTPError);
			fileLoader.removeEventListener ( IOErrorEvent.IO_ERROR,OnFileLoadingIOError );
			var _xmlDatas:XML = new XML( _evt.target.data);
			endFileLoading(_xmlDatas);
						
		}
		private static function endFileLoading(_datas:*):void{
		//appel de la fonction de traitement référencée dans la fonction de chargement
			var res:Array = m_fileArray.shift();//on enleve la premiere entree du tableau que l'on stoque dans une variable locale
			if(res[1]){
				res[1](_datas);
			}else{
				T.Trace ('[FileLoader]{endLoading} Fatal Error CallBackFunction : Does not exist ');
			}
			fileLoader=null;
			if(m_fileArray.length >0){
				startFileDownload();
			}
		}
		/////////////////////////////////////
		//Fonctions de gestion des erreurs //
		/////////////////////////////////////
		private static function OnFileHTTPError(_evt:HTTPStatusEvent):void{
			//T.Trace("[FileLoader] Evenement HTTP :"+_evt.toString()+" \n");
		}
		private static function OnFileLoadingIOError(_evt:IOErrorEvent):void{
			T.Trace("[FileLoader]Erreur fatale de chargement \n\nErreur : \n"+_evt.toString());
		}
	}
	
}